package com.shimao.celnet.payment.controller.apinotcheck;

import com.baiwang.bop.client.BopException;
import com.baiwang.bop.client.IBopClient;
import com.baiwang.bop.client.impl.BopRestClient;
import com.baiwang.bop.request.impl.bizinfo.CompanySearchRequest;
import com.baiwang.bop.respose.entity.bizinfo.CompanySearchResponse;
import com.baiwang.bop.utils.JacksonUtil;
import com.shimao.celnet.payment.model.request.SCodeNameConstant;
import com.shimao.celnet.payment.service.apiservice.CallOut_invOpenOutService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;




/**
 * @Author: christ
 * @Description: 获取抬头信息接口；外部系统--》支付平台
 * */

@RestController
@RequestMapping(value = "/invoice")
public class CallOut_invCompSearchOutController {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private CallOut_invOpenOutService invOpenOutService;

    @RequestMapping(value = "/title", method = RequestMethod.POST)
    public Object getTitle(@RequestParam(value = "companyName")String companyName, @RequestParam(value = "taxId") String taxId){
        CompanySearchRequest searchRequest = new CompanySearchRequest();
        CompanySearchRequest request = new CompanySearchRequest();

        request.setCompanyName(companyName);//沙箱环境只能获取“百望股份”相关信息 --企业名称
        request.setAccuracy("FALSE");//是否精确查找；TRUE表示精确查找;FALSE表示模糊查找；默认false
        request.setSort("{\"frequency\":0}");//排序方式；0表示降序，1表示升序；
        request.setTaxId(taxId==null?"":taxId);//企业税号
        request.setAppId("");
        String responseStr="";
        try
        {
            String token = invOpenOutService.getToken();
            CompanySearchResponse response = this.getClient().execute(request,token,CompanySearchResponse.class);
            //如果查询结果为空，则提示抬头不存在
            if(!("[]".equals(JSONArray.fromObject(JSONObject.fromObject(response).get("result")).toString()))) {
                return JSONObject.fromObject(response).get("result");
            }
            logger.info(JacksonUtil.beanToString(response));
            logger.info("success");
        }
        catch ( BopException e)
        {
            logger.info("error");
        }
     return "fail";
    }
    public  IBopClient getClient(){
        SCodeNameConstant sCodeNameConstant =invOpenOutService.getBwSettingInfo();
        IBopClient client = new BopRestClient(sCodeNameConstant.getRateUrl(), sCodeNameConstant.getKey(), sCodeNameConstant.getSecret());
        return client;
    }

}
